/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.rw.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.google.common.collect.Lists;
import com.hcht.xrlh.drhis.dao.DrHisDao;
import com.hcht.xrlh.drhis.entity.DrHis;
import com.hcht.xrlh.jcjg.dao.DrJgDao;
import com.hcht.xrlh.jcjg.entity.DrJg;
import com.hcht.xrlh.jh.dao.DrJhDao;
import com.hcht.xrlh.jh.entity.DrJh;
import com.hcht.xrlh.rw.entity.DrRw;
import com.hcht.xrlh.rw.dao.DrRwDao;
import com.hcht.xrlh.rwfz.dao.DrRwXzDao;
import com.hcht.xrlh.rwfz.dao.DrRwXzyrDao;
import com.hcht.xrlh.rwfz.dao.DrXzRwDao;
import com.hcht.xrlh.rwfz.entity.DrRwXz;
import com.hcht.xrlh.rwfz.entity.DrRwXzyr;
import com.hcht.xrlh.rwfz.entity.DrXzRw;
import com.hcht.xrlh.rwfz.service.DrRwXzService;
import com.hcht.xrlh.sx.dao.DrSxDao;
import com.hcht.xrlh.sx.entity.DrSx;
import com.hcht.xrlh.zfdx.entity.DrZfdx;
import com.hcht.xrlh.zfdxk.dao.DrZfdxkDao;
import com.hcht.xrlh.zfdxk.entity.DrZfdxk;
import com.hcht.xrlh.zfryk.dao.DrZfrykDao;
import com.hcht.xrlh.zfryk.entity.DrZfryk;

/**
 * 检查任务Service
 * @author lrz
 * @version 2016-12-21
 */
@Service
@Transactional(readOnly = true)
public class DrRwService extends CrudService<DrRwDao, DrRw> {

	@Autowired
	DrZfdxkDao dxDao;
	
	@Autowired
	DrZfrykDao ryDao;
	
	@Autowired
	DrJhDao jhDao;
	
	@Autowired
	DrSxDao sxDao;
	
	@Autowired
	DrRwXzDao rwXzDao;
	
	@Autowired
	DrRwXzService drRwXzService;
	
	@Autowired
	DrRwDao drRwDao;
	
	@Autowired
	DrRwXzyrDao drRwXzyrDao;
	
	@Autowired
	DrXzRwDao drXzRwDao;
	
	@Autowired
	DrJgDao drJgDao;
	
	@Autowired
	DrHisDao drHisDao;
	public DrRw get(String id) {
		return super.get(id);
	}
	
	public List<DrRw> findList(DrRw drRw) {
		return super.findList(drRw);
	}
	
	public Page<DrRw> findPage(Page<DrRw> page, DrRw drRw) {
		drRw.setPage(page);
		List<DrRw> list = drRwDao.findList(drRw);
		for(DrRw rw:list){
			String users = "";
			String xds = "";
			DrRwXzyr q_ry = new DrRwXzyr();
			q_ry.setRwId(rw.getId());
			List<DrRwXzyr> ryList = drRwXzyrDao.findList(q_ry);
			for(DrRwXzyr ry:ryList){
				users = users+ry.getUser().getName()+"、";
			}
			if(users.indexOf("、")>0){
				users = users.substring(0,users.lastIndexOf("、"));
			}
			rw.setRyMcs(users);
			
			DrXzRw q_dx = new DrXzRw();
			q_dx.setRwId(rw.getId());
			List<DrXzRw> dxList = drXzRwDao.findList(q_dx);
			for(DrXzRw dx : dxList){
				xds = xds + dx.getCol1()+"、";
			}
			if(xds.indexOf("、")>0){
				xds = xds.substring(0,xds.lastIndexOf("、"));
			}
			rw.setDxMcs(xds);
		}
		page.setList(list);
		return page;
	}
	
	@Transactional(readOnly = false)
	public String save(DrRw drRw,String uids) {
		//保存任务表
		//String rwId = "";
		String rwId = super.save_r(drRw);
		String sxId = drRw.getSxId();
		String jhId = drRw.getJhId();
		DrSx sx = sxDao.get(sxId);
		
		//人员库去掉不参加人员
		List<String> ids = Arrays.asList(uids.split(","));
		
		//获得执法人员库
		DrZfryk drZfryk = new DrZfryk();
		drZfryk.setSxId(sxId);
		List<DrZfryk> ry_list = ryDao.findList(drZfryk);
		List<DrZfryk> ry_in_list = Lists.newArrayList();
		for(DrZfryk ry:ry_list){
			if(!ids.contains(ry.getId())){
				ry_in_list.add(ry);
			}
		}
		int inUseRyNum = ry_in_list.size();
		//获取执法对象库
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		List<DrZfdxk> dx_list = dxDao.findList(dx);
		int inUseDxNum = dx_list.size();
		DrJh jh = jhDao.get(jhId);
		//小组数
		int xzsl = jh.getJhXzsl().intValue();
		//每组人数
		int mzrs = jh.getJhMzrs().intValue();
		//对象抽取方式
		String cqfs = jh.getJhDxcqfs();
		//对象抽取数量
		int cqsl = jh.getJhDxcqsl().intValue();
		//抽取总人数
		int total_ry = xzsl*mzrs;
		//抽取总对象数
		int total_dx = 0;
		if("01".equals(cqfs)){
			total_dx = cqsl;
		}else if("00".equals(cqfs)){
			total_dx = cqsl*dx_list.size()/100;
		}
		if(ry_in_list.size()<total_ry){
			System.out.println("执法人员库可用人员过少，不满足抽取需要。");
			//throw new ServiceException("执法人员库可用人员过少，不满足抽取需要。");
		}
		if(dx_list.size()<total_dx){
			System.out.println("执法对象库可用对象过少，不满足抽取需要。");
		}
/*		for(DrZfryk ry:ry_in_list){
			System.out.println("============="+ry.getUser().getName());
		}*/
		//抽取执法人员
		List<DrZfryk> select_r = Lists.newArrayList();
		do {
			int m =(int)(Math.random()*ry_in_list.size());
			DrZfryk ren = ry_in_list.get(m);
			select_r.add(ren);
			ry_in_list.remove(ren);
			total_ry = total_ry-1;
		} while (total_ry>0);
		
		
		
		//抽取执法对象
		List<DrZfdxk> select_dx = Lists.newArrayList();
		do {
			int n =(int)(Math.random()*dx_list.size());
			DrZfdxk zfdx = dx_list.get(n);
			select_dx.add(zfdx);
			dx_list.remove(zfdx);
			total_dx = total_dx-1;
		} while (total_dx>0);
		
		List<List<DrXzRw>> getDxGroup = getDxGroup(select_dx,xzsl,rwId,jhId,sxId);
				
		//保存任务小组
		for(int i=1;i<=xzsl;i++){
			DrRwXz drRwXz = new DrRwXz();
			drRwXz.setRwId(rwId); 
			drRwXz.setJhId(jhId);
			drRwXz.setSxId(sxId);
			drRwXz.setXzIndex((long)i);
			drRwXz.setRemarks(getXzName(i));
			//任务组执法人员
			List<DrRwXzyr> userInGroup = Lists.newArrayList();
			for(int j=(i-1)*mzrs;j<(i)*mzrs;j++){
				DrRwXzyr drRwXzyr = new DrRwXzyr();
				drRwXzyr.setId("");
				drRwXzyr.setDelFlag("0");
				drRwXzyr.setRwId(rwId);
				drRwXzyr.setJhId(jhId);
				drRwXzyr.setSxId(sxId);
				drRwXzyr.setUser(select_r.get(j).getUser());
				userInGroup.add(drRwXzyr);
			}
			//任务组执法对象
			List<DrXzRw> dxInGroup = getDxGroup.get(i-1);
			
			drRwXz.setDrRwXzyrList(userInGroup);
			drRwXz.setDrXzRwList(dxInGroup);
			//保存任务小组
			//rwXzDao.insert(entity)
			drRwXzService.save(drRwXz);
		}
		
		//保存抽取历史记录
		DrHis dh = new DrHis();
		dh.setRwId(rwId);
		dh.setZfyys((long)inUseRyNum);
		dh.setZfdxs((long)inUseDxNum);
		dh.setFlag("n");
		
		dh.preInsert();
		drHisDao.insert(dh);
		
		return rwId;
	}
	
	public List<String> checkRyDx(DrRw drRw,String uids){
		List<String> result=Lists.newArrayList();
		String sxId = drRw.getSxId();
		String jhId = drRw.getJhId();
		DrSx sx = sxDao.get(sxId);
		
		//人员库去掉不参加人员
		List<String> ids = Arrays.asList(uids.split(","));
		
		//获得执法人员库
		DrZfryk drZfryk = new DrZfryk();
		drZfryk.setSxId(sxId);
		List<DrZfryk> ry_list = ryDao.findList(drZfryk);
		List<DrZfryk> ry_in_list = Lists.newArrayList();
		for(DrZfryk ry:ry_list){
			if(!ids.contains(ry.getId())){
				ry_in_list.add(ry);
			}
		}
		
		//获取执法对象库
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		List<DrZfdxk> dx_list = dxDao.findList(dx);
		DrJh jh = jhDao.get(jhId);
		//小组数
		int xzsl = jh.getJhXzsl().intValue();
		//每组人数
		int mzrs = jh.getJhMzrs().intValue();
		//对象抽取方式
		String cqfs = jh.getJhDxcqfs();
		//对象抽取数量
		int cqsl = jh.getJhDxcqsl().intValue();
		//抽取总人数
		int total_ry = xzsl*mzrs;
		//抽取总对象数
		int total_dx = 0;
		if("01".equals(cqfs)){
			total_dx = cqsl;
		}else if("00".equals(cqfs)){
			total_dx = cqsl*dx_list.size()/100;
		}
		if(ry_in_list.size()<total_ry){
			//System.out.println("执法人员库可用人员过少，不满足抽取需要。");
			//throw new ServiceException("执法人员库可用人员过少，不满足抽取需要。");
			result.add("执法人员库可用人员过少，不满足抽取需要");
		}
		if(dx_list.size()<total_dx){
			//System.out.println("执法对象库可用对象过少，不满足抽取需要。");
			result.add("执法对象库可用对象过少，不满足抽取需要");
		}		
		return result;
	}
	
	public List<List<DrXzRw>> getDxGroup(List<DrZfdxk> select_dx,int fenzu,String rwId,String jhId,String sxId){
		int zongshu= select_dx.size();
		int meizu = zongshu/fenzu;
		List<List<DrXzRw>> groups = Lists.newArrayList();
		
		for(int i=0;i<fenzu;i++){
			List<DrZfdxk> _list = select_dx.subList(0, meizu);
			List<DrXzRw> list_new = Lists.newArrayList();
			for(DrZfdxk drZfdxk:_list){
				DrXzRw drXzRw = new DrXzRw();
				drXzRw.setId("");
				drXzRw.setDelFlag("0");
				drXzRw.setRwId(rwId);
				drXzRw.setJhId(jhId);
				drXzRw.setSxId(sxId);
				drXzRw.setDxId(drZfdxk.getId());
				list_new.add(drXzRw);
			}
			groups.add(list_new);
			for(int j=0;j<meizu;j++){
				select_dx.remove(0);
			}
		}
		if(select_dx.size()>0){
			for(int m=0;m<select_dx.size();m++){
				DrXzRw drXzRw = new DrXzRw();
				drXzRw.setId("");
				drXzRw.setDelFlag("0");
				drXzRw.setRwId(rwId);
				drXzRw.setJhId(jhId);
				drXzRw.setSxId(sxId);
				drXzRw.setDxId(select_dx.get(m).getId());
				groups.get(m).add(drXzRw);
			}
		}
		return groups;
	}
	
	@Transactional(readOnly = false)
	public void delete(DrRw drRw) {
		super.delete(drRw);
		//删除任务小组
		rwXzDao.delRwxzByRwid(drRw.getId());
		//删除任务小组人员
		drRwXzyrDao.delByRwid(drRw.getId());
		//删除任务小组任务
		drXzRwDao.delByRwid(drRw.getId());
		//删除结果
		DrJg drJg = new DrJg();
		drJg.setRwId(drRw.getId());
		List<DrJg> jg_list = drJgDao.findList(drJg);
		if(jg_list.size()>0){
			drJgDao.delete(jg_list.get(0));
		}
	}
	
	public String getXzName(int i){
		String name="";
		switch (i) {
		case 1:
			name = "一组";
			break;
		case 2:
			name = "二组";
			break;	
		case 3:
			name = "三组";
			break;	
		case 4:
			name = "四组";
			break;	
		case 5:
			name = "五组";
			break;	
		case 6:
			name = "六组";
			break;	
		case 7:
			name = "七组";
			break;	
		case 8:
			name = "八组";
			break;	
		case 9:
			name = "九组";
			break;	
		case 10:
			name = "十组";
			break;	
		default:
			break;
		}
		return name;
	}
	
	@Transactional(readOnly = false)
	public void update(DrRw drRw) {
		// TODO Auto-generated method stub
		drRwDao.update(drRw);
	}
}